// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// T [author] rhyme_lph
// O [email]  rhymelph@gmail.com
// y [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/entities/account_entity.dart';
import 'package:finance_app/src/utils/date_time_utils.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/form/value_select_form.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/input/form_input_field.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/dashboard/bank/add_bank_account_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:flutter_svg/flutter_svg.dart';

@RouterMeta(paramName: "addBankAccountPage")
class AddBankAccountPage extends StatefulWidget {
  final AccountEntity data;

  const AddBankAccountPage({Key key, this.data}) : super(key: key);

  @override
  _AddBankAccountPageState createState() => _AddBankAccountPageState();
}

class _AddBankAccountPageState extends State<AddBankAccountPage>
    with PagePresenter<AddBankAccountPage, AddBankAccountProvider> {
  @override
  void initState() {
    provider.initAccount(widget.data);
    super.initState();
  }

  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
      title:
          '${provider.id == null ? S.current.add_bank_account : S.current.edit_bank_account}');

  @override
  Widget get buildWidget => ListView(
        children: [
          FromInputField(
            tip: S.current.bank_name,
            controller: provider.bankNameController,
            focusNode: provider.bankNameFocusNode,
            keyboardType: TextInputType.text,
          ),
          FromInputField(
            key: ValueKey(S.current.bank_account_number),
            tip: S.current.bank_account_number,
            controller: provider.bankAccountNumberController,
            focusNode: provider.bankAccountNumberFocusNode,
            keyboardType: TextInputType.numberWithOptions(decimal: true),
          ),
          listBuilder<String>(
            selector: (_, __) => __.selectCurrency?.name,
            builder: (BuildContext context, String value, Widget child) =>
                ValueSelectForm(
              title: S.current.currency,
              value: value,
              onTap: provider.hasUsed ? null : provider.onChangeCurrency,
            ),
          ),
          listBuilder<bool>(
            selector: (_, __) => __.id != null,
            builder: (BuildContext context, bool value, Widget child) =>
                value == true
                    ? ValueSelectForm(
                        value: provider.openingBalanceController.text,
                        title: S.current.opening_balance,
                      )
                    : FromInputField(
                        key: ValueKey(S.current.opening_balance),
                        tip: S.current.opening_balance,
                        controller: provider.openingBalanceController,
                        focusNode: provider.openingBalanceFocusNode,
                        keyboardType:
                            TextInputType.numberWithOptions(decimal: true),
                      ),
          ),
          Stack(
            children: [
              listBuilder<DateTime>(
                selector: (_, __) => __.asAt,
                builder: (BuildContext context, DateTime value, Widget child) =>
                    ValueSelectForm(
                  title: S.current.as_at,
                  value: DateTimeUtils.formatGlobalDateTime(value),
                  // onTap: provider.isEnableAsAt ? provider.onChangeAsAt : null,
                  onTap: provider.hasUsed ? null : provider.onChangeAsAt,
                ),
              ),
              Positioned(
                top: 0,
                bottom: 0,
                right: getDp(35),
                child: PressedOpacityButton(
                  onPressed: provider.onShowAsOnDateTip,
                  child: SvgPicture.asset(
                    A.assetsImagesStyleInformation_1,
                    width: getDp(32),
                    height: getDp(32),
                    fit: BoxFit.contain,
                  ),
                ),
              )
            ],
          ),
          AppSpaces.h30,
          NormalButton(
            title: provider.id == null ? S.current.add : S.current.done,
            onTap: provider.onAdd,
          ),
        ],
      );
}
